import 'package:app/components/my_webview.dart';
import 'package:app/pages/document_module/document_model.dart';
import 'package:app/pages/document_module/file_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../services/address.dart';

class DocumentController extends GetxController{


  // MyFolderModel? model;

  List folderArr = [];


  int page = 1;



  /// 我的文件夹
  requestDataWithFolder()async{

    var json = await DioManager().kkRequest(Address.documentFolder,method: 'get');

    MyFolderModel model1 = MyFolderModel.fromJson(json);

    folderArr.clear();

    if(model1.data !=null){
      folderArr.addAll(model1.data!);
    }

    update();
  }

  /// 删除文件夹或文件
  Future requestDataWithUnlink({var type = '文件夹',var document_id})async{
    var params = {
      'document_id':document_id,
      'type':type,
    };
    var json = await DioManager().kkRequest(Address.documentFolderUnlink,bodyParams: params);

    BotToast.showText(text: json['message']);

    return json;
  }
  /// 重命名
  Future requestDataWithReName({var type = '文件夹',var document_id,var name})async{
    var params = {
      'type':type,
      'document_id':document_id,
      'name':name,
    };
    var json = await DioManager().kkRequest(Address.reNameDocument,bodyParams: params);
    BotToast.showText(text: json['message']);
    return json;
  }
  /// 弹框
  dialogRename(){
    BrnMiddleInputDialog(
        title: '重命名',
        hintText: '请输入名字',
        cancelText: '取消',
        confirmText: '确定',
        maxLength: 1000,
        maxLines: 2,
        barrierDismissible: false,
        inputEditingController: TextEditingController()..text = '',
        textInputAction: TextInputAction.done,
        onConfirm: (value) {

          // BrnToast.show(value, Get.overlayContext!);
        },
        onCancel: () {
          Get.back();
          // BrnToast.show("取消", context);
          // Navigator.pop(context);
        }).show(Get.overlayContext!);
  }

  onRefresh()async{

    requestDataWithFolder();
  }

  onLoad()async{

  }


  /// 点击进入文件夹
  tapClickDocument(var id)async{

    Get.toNamed(AppRoutes.documentFile,arguments:id);
  }
  /// 点击进入创建文件夹
  tapClickCreateDocument()async{
   var data = await Get.toNamed(AppRoutes.createFolder);

   if(data == 'refresh'){
     requestDataWithFolder();
   }
  }



  /// 获取上传地址
  ///
  requestDataWithUpLoader(var folder_id)async{
    var params = {
      'model_name':'documents.document',
      'res_id':'',
      'folder_id':folder_id,
      'field':'',
    };
    var json = await DioManager().kkRequest(Address.upLoaderUrl,params: params,method: 'get');

    Get.to(MyWebView('', json['data']));

    return json['data'];
  }


  tapClickGetLoader()async{

  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestDataWithFolder();
  }
}